﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.ObjectBuilder;
using DemoSupport;

namespace MenuDemo
{
	/// <summary>
	/// Interaction logic for MenuDemoView.xaml
	/// </summary>
	public partial class MenuDemoView : DemoViewBase, IMenuDemoView
	{
		public static readonly DependencyProperty PresenterProperty = DependencyProperty.Register("Presenter",
			typeof(MenuDemoPresenter),
			typeof(MenuDemoView),
			new FrameworkPropertyMetadata(Presenter_Changed));

		[Dependency]
		public MenuDemoPresenter Presenter
		{
			get
			{
				return GetValue(PresenterProperty) as MenuDemoPresenter;
			}
			set
			{
				SetValue(PresenterProperty, value);
			}
		}

		public ImageSource MenuItemImage
		{
			get
			{
				return FindResource("MenuItemImage") as ImageSource;
			}
		}

		public MenuDemoView()
		{
			InitializeComponent();
		}

		private static void Presenter_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
		{
			if (e.NewValue != null)
			{
				(e.NewValue as MenuDemoPresenter).View = dependencyObject as MenuDemoView;
			}
		}

		#region Example 1

		public static readonly RoutedCommand Example1AddTopLevelMenuItem = new RoutedCommand("Example1AddTopLevelMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example1AddChildMenuItem = new RoutedCommand("Example1AddChildMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example1RemoveChildMenuItem = new RoutedCommand("Example1RemoveChildMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example1RemoveTopLevelMenuItem = new RoutedCommand("Example1RemoveTopLevelMenuItem", typeof(MenuDemoView));

		private void Example1AddTopLevelMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = (Presenter != null) && (Presenter.Example1CurrentStep == MenuDemoPresenter.Example1Step.Start);
		}

		private void Example1AddTopLevelMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example1AddTopLevelMenuItem();
		}

		private void Example1AddChildMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = (Presenter != null) && (Presenter.Example1CurrentStep == MenuDemoPresenter.Example1Step.AddedTopLevelMenuItem);
		}

		private void Example1AddChildMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example1AddChildMenuItem();
		}

		private void Example1RemoveChildMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = (Presenter != null) && (Presenter.Example1CurrentStep == MenuDemoPresenter.Example1Step.AddedChildMenuItem);
		}

		private void Example1RemoveChildMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example1RemoveChildMenuItem();
		}

		private void Example1RemoveTopLevelMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = (Presenter != null) && ((Presenter.Example1CurrentStep == MenuDemoPresenter.Example1Step.AddedTopLevelMenuItem) || (Presenter.Example1CurrentStep == MenuDemoPresenter.Example1Step.AddedChildMenuItem));
		}

		private void Example1RemoveTopLevelMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example1RemoveTopLevelMenuItem();
		}

		#endregion

		#region Example 2

		public static readonly RoutedCommand Example2AddMenuItemWithImage = new RoutedCommand("Example2AddMenuItemWithImage", typeof(MenuDemoView));

		public static readonly RoutedCommand Example2AddCheckableMenuItem = new RoutedCommand("Example2AddCheckableMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example2CheckUncheckMenuItem = new RoutedCommand("Example2CheckUncheckMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example2CleanUp = new RoutedCommand("Example2CleanUp", typeof(MenuDemoView));

		private void Example2AddMenuItemWithImage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example2CanAddMenuItemWithImage();
		}

		private void Example2AddMenuItemWithImage_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example2AddMenuItemWithImage();
		}

		private void Example2AddCheckableMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example2CanAddCheckableMenuItem();
		}

		private void Example2AddCheckableMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example2AddCheckableMenuItem();
		}

		private void Example2CheckUncheckMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example2CanCheckUncheckMenuItem();
		}

		private void Example2CheckUncheckMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example2CheckUncheckMenuItem();
		}

		private void Example2CleanUp_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example2CanCleanUp();
		}

		private void Example2CleanUp_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example2CleanUp();
		}

		#endregion

		#region Example 3

		public static readonly RoutedCommand Example3AddSortedMenuItem = new RoutedCommand("Example3AddSortedMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example3AddGroupedMenuItem = new RoutedCommand("Example3AddGroupedMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example3AddGroupedAndSortedMenuItems = new RoutedCommand("Example3AddGroupedAndSortedMenuItems", typeof(MenuDemoView));

		public static readonly RoutedCommand Example3CleanUp = new RoutedCommand("Example3CleanUp", typeof(MenuDemoView));

		private void Example3AddSortedMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example3CanAddSortedMenuItem();
		}

		private void Example3AddSortedMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example3AddSortedMenuItem();
		}

		private void Example3AddGroupedMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example3CanAddGroupedMenuItem();
		}

		private void Example3AddGroupedMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example3AddGroupedMenuItem();
		}

		private void Example3AddGroupedAndSortedMenuItems_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example3CanAddGroupedAndSortedMenuItems();
		}

		private void Example3AddGroupedAndSortedMenuItems_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example3AddGroupedAndSortedMenuItems();
		}

		private void Example3CleanUp_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example3CanCleanUp();
		}

		private void Example3CleanUp_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example3CleanUp();
		}

		#endregion

		#region Example 4

		public static readonly RoutedCommand Example4AddParentMenuItem = new RoutedCommand("Example4AddParentMenuItem", typeof(MenuDemoView));

		public static readonly RoutedCommand Example4AddChildMenuItems = new RoutedCommand("Example4AddChildMenuItems", typeof(MenuDemoView));

		public static readonly RoutedCommand Example4CleanUp = new RoutedCommand("Example4CleanUp", typeof(MenuDemoView));

		private void Example4AddParentMenuItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example4CanAddParentMenuItem();
		}

		private void Example4AddParentMenuItem_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example4AddParentMenuItem();
		}

		private void Example4AddChildMenuItems_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example4CanAddChildMenuItems();
		}

		private void Example4AddChildMenuItems_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example4AddChildMenuItems();
		}

		private void Example4CleanUp_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = Presenter.Example4CanCleanUp();
		}

		private void Example4CleanUp_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			Presenter.Example4CleanUp();
		}

		#endregion

		public void ShowChildMenuItemClicked()
		{
			MessageBox.Show("This message was displayed via the command handler for the child menu item.");
		}
	}
}
